﻿//Registers click event when the page initially loads
$(document).ready(function () {
    $('#reoccuringChkB').click(function () {
        if ($('#reoccuringChkB').is(":checked")) //Shows the Reoccuring label/dropdown when checkbox is marked
        {
            $('#reoccuringTypeCB').show('slow');
            $('#reoccuringTypeL').show('slow');
        }
        else //Hides the two controls when checkbox is not marked
        {
            $('#reoccuringTypeCB').hide('slow');
            $('#reoccuringTypeL').hide('slow');
            $('#reoccuringTypeCB').val("");
        }
    });
    
});
//Returns the data object that has all of the reminder information the user typed in
//Used to capture the data that gets sent to the server for processing
function GetReminderData()
{
    var subject = $('#subjectTF').val();
    var message = $('#contentTA').val();
    var hourCB = $('#hourCB').val();
    var minCB = $('#minuteCB').val();
    var date = $('#dateChooser').val();
    var reoccuringType = $('#reoccuringTypeCB option:selected').val();
    var isReoccuring;
    var am_pm;

    if ($('#amRB').is(':checked') == true)
        am_pm = "AM";
    else
        am_pm = "PM";

    if ($('#reoccuringChkB').is(":checked") == true)
        isReoccuring = true;
    else
        isReoccuring = false;

    var data = new Object();
    data.jSubject = subject;
    data.jReminderText = message;
    data.jRemindDate = date;
    data.jHour = hourCB;
    data.jMinute = minCB;
    data.jAM_PM = am_pm;
    data.jReoccuringType = reoccuringType;
    data.jRecursive = isReoccuring;

    return data;
}
//Opens the Reminders dialog box
//Displays either Create/Update buttons depending on if data is already loaded into it
//Data gets loaded when the Edit...link event is fired
function OpenDialog()
{
    var id = $('#requestIDTF').val();
    var url;
    var buttonName;
    var paramData = GetReminderData();
    
    if (id == 0)
    {
        url = "MainPage.aspx/CreateReminder";
        buttonName = "Create";
    }
    else
    {
        url = "MainPage.aspx/UpdateReminder";
        buttonName = "Update";
        paramData.jID = id;
    }
    //Function that displays an image on the dateChooser button
    $(function () {
        var pickerOptions = {
            showOn: "button",
            buttonImage: "Images/datePicker.gif"
        }
        $('#dateChooser').datepicker(pickerOptions);
    });

    var dialog_buttons = {};
    dialog_buttons[buttonName] = function () {
        paramData = GetReminderData(); //Pulls the data entered in by the user and retrieves data object

        if (id.length != 0)
            paramData.jID = id;

        var params = JSON.stringify(paramData); //JSON method that automatically puts the data in JSON form
       
        $.ajax({ //AJAX call that sends data to the server for processing and returns Success or Error
            type: "POST",
            url: url,
            data: params,
            contentType: "application/json",
            dataType: "json",
            success: function (msg) { //Closes the dialog box and refreshes the screen
                $('#createReminderDialog').dialog("close");
                document.location.reload(true);
            },
            error: function (msg) { //Throws error message to the client
                alert(msg.d);
                $('#createReminderDialog').dialog("close");
            }
        });
    }
    dialog_buttons["Cancel"] = function () {
        $('#createReminderDialog').dialog("close");
    }

    var window = $('#createReminderDialog').dialog({ //Displays the createReminderDialog div element as a dialog
        autoopen: false,
        height: 520,
        width: 510,
        modal: true,
        show: 'slow',
        hide: 'slow',
        resizable: false,
        overlay: {
            opacity: 0.9,
            background: 'black'
        },
        buttons: dialog_buttons
    }).parent().appendTo("form"); //Appends the dialog to the form element so the server can process data
    window.dialog('open'); //Opens the window
    return false;
}
//Ensures that all values are clear when the New... link is clicked before opening the dialog window
function CreateNewReminder()
{
    ClearValues();
    OpenDialog();
}
//AJAX function that pulls data from the server into the dialog controls
function LoadReminderData()
{
    if ($('#remindersLB').val() == null) //If nothing was selected this statement throws an error and exits the function
    {
        alert("No reminder selected to load");
        return;
    }
        
    $.ajax({ //AJAX call that pulls data from SQL and loads them into each ASP element
        type: "POST",
        url: "MainPage.aspx/GetReminderData",
        data: "{'jReminderSubject':'" + $('#remindersLB').val() + "'}",
        dataType: "json",
        contentType: "application/json",
        success: function (msg) {
            var data;
            var hours;
            var minutes;
            
            $.each(msg, function (index, value) {
                var id = msg.d[0];
                var subject = msg.d[1];
                var content = msg.d[2];
                var date = msg.d[3];
                var time = msg.d[4];
                var isRecursive = msg.d[5];
                var recursionType = msg.d[6];
                var hours;
                var times;
                
                if (time.length == 7) //If the time has a one digit hour
                {
                    hours = time.charAt(0);
                    minutes = time.substring(2, 4);
                }
                else //If the time has a two digit hour
                {
                    hours = time.substring(0, 2);
                    minutes = time.substring(3, 4);
                }

                $('#requestIDTF').val(id);
                $('#subjectTF').val(subject);
                $('#contentTA').val(content);
                $('#hourCB').val(hours);
                $('#minuteCB').val(minutes);
                $('#dateChooser').val(date);
                $('#reoccuringTypeCB').val(recursionType);

                if (isRecursive == true) //Show the elements if isRecursive SQL column is true
                {
                    $('#reoccuringChkB').attr('checked', true);
                    $('#reoccuringTypeL').show();
                    $('#reoccuringTypeCB').show();
                }
                else //If it isn't true, hide the controls
                {
                    $('#reoccuringChkB').attr('checked', false);
                    $('#reoccuringTypeL').hide();
                    $('#reoccuringTypeCB').hide();
                }
                if (time.indexOf("AM") >= 0) //If the time has "AM" in the string, check the AM box
                    $('#amRB').attr('checked', true);
                else //If the time does not have "AM" in the string, check the PM box
                    $('#pmRB').attr('checked', true);
                OpenDialog(); //Opens the dialog window after elements are loaded in
            });
            
            
        },
        error: function (msg) { //Throws error if the AJAX call fails
            alert(msg.status + ' ' + msg.statusText);
        }
    });
}
//Sets the fulfilled flag in SQL to true indicating the reminder is no longer needed
function RemoveReminder()
{
    var reminder = $('#remindersLB').val();
    
    if (reminder == null) //Throws an error if there is no reminder selected in the list
    {
        alert("No reminder selected to remove");
        return;
    }

    $.ajax({ //Ajax call to the server to set the flag
        type: "POST",
        url: "MainPage.aspx/RemoveReminder",
        data: "{'jReminder':'" + reminder + "'}",
        contentType: "application/json",
        dataType: "json",
        success: function () { //Refreshes the page after it completes
            document.location.reload(true);
        },
        error: function (msg) { //Displays error from server if it fails
            alert(msg.d);
        }
    });
}
//Clears all of the values from all controls in the dialog window
function ClearValues()
{
    $('#requestIDTF').val("");
    $('#subjectTF').val("");
    $('#contentTA').val("");
    $('#hourCB').val("1");
    $('#minuteCB').val("00");
    $('#dateChooser').val("");
    $('#amRB').attr('checked', false);
    $('#pmRB').attr('checked', false);
    $('#reoccuringChkB').attr('checked', false);
    $('#reoccuringTypeL').hide();
    $('#reoccuringTypeCB').hide();
    $('#reoccuringTypeCB').val("");
}
